//package com.infra.server.controller;
//
//import com.infra.server.utils.ConstantUtil;
//import com.infra.server.utils.EsUtil;
//import io.swagger.annotations.Api;
//import lombok.extern.slf4j.Slf4j;
//import org.elasticsearch.action.search.SearchRequest;
//import org.elasticsearch.action.search.SearchResponse;
//import org.elasticsearch.client.RequestOptions;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.elasticsearch.rest.RestStatus;
//import org.elasticsearch.search.aggregations.AggregationBuilder;
//import org.elasticsearch.search.aggregations.AggregationBuilders;
//import org.elasticsearch.search.aggregations.Aggregations;
//import org.elasticsearch.search.aggregations.bucket.terms.Terms;
//import org.elasticsearch.search.builder.SearchSourceBuilder;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import java.io.IOException;
//import java.util.List;
//
///**
// * @author win
// * @date 2021/9/9 15:54
// */
//@Slf4j
//@Api(value = "ESController",tags = "elasticsearch相关操作接口")
//@RestController
//@RequestMapping("/es")
//public class ESController {
//
//    @Autowired
//    private EsUtil esUtil;
//
//    @Qualifier("client")
//    @Autowired
//    private RestHighLevelClient client;
//
//    @GetMapping("/")
//    public void test() {
//        String responseResult = "";
//        try {
//            AggregationBuilder aggr = AggregationBuilders.terms("age_bucket").field("requestUrl.keyword").size(1000);
//            // 查询源构建器
//            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//            searchSourceBuilder.aggregation(aggr);
//            // 创建查询请求对象，将查询条件配置到其中
//            SearchRequest request = new SearchRequest(ConstantUtil.ELASTIC_USER_RECORD);
//            request.source(searchSourceBuilder);
//            // 执行请求
//            SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//            // 获取响应中的聚合信息
//            Aggregations aggregations = response.getAggregations();
//            // 输出内容
//            if (RestStatus.OK.equals(response.status())) {
//                // 分桶
//                Terms byCompanyAggregation = aggregations.get("age_bucket");
//                List<? extends Terms.Bucket> buckets = byCompanyAggregation.getBuckets();
//                // 输出各个桶的内容
//                log.info("-------------------------------------------");
//                log.info("聚合信息:");
//                for (Terms.Bucket bucket : buckets) {
//                    log.info("桶名：{} | 总数：{}", bucket.getKeyAsString(), bucket.getDocCount());
//                }
//                log.info("-------------------------------------------");
//            }
//        } catch (IOException e) {
//            log.error("", e);
//        }
//    }
//}
